﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using School.Accounting;

namespace School.Pages.Accounting.Reports
{
    public partial class showStudentAccountBalance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["studentInfo"] != null && !IsPostBack)
            {
                StudentInfo studentInfo = (StudentInfo)Session["studentInfo"];
                long campus = studentInfo.Campus;
                long certificate = studentInfo.Certificate;
                long stage = studentInfo.Stage;
                long grade = studentInfo.Grade;
                int xclass = studentInfo.XClass;
                long student = studentInfo.Student;
                List<long> expenses = studentInfo.Expenses;
                if (student == 0)
                    return;

                var balance = LatePayment.GetBalance(campus, certificate, stage, grade, xclass, student)
                    .Select(x => new { Student_Name = x.TE_Student.Ar_FirstName + " " + x.TE_Student.Ar_SecondName + " " + x.TE_Student.Ar_ThirdNmae + " " + x.TE_Student.Ar_FourthName, Student_Number = x.TE_Student.RegistrationNumber, x.Value, Student_Id = x.TE_Student.Id, Type = 1 });
                bool showBalance = balance.Count() > 0 ? true : false;
                ReportDataSource rdsBalance = new ReportDataSource("Balance", balance);

                var incompeteVoucher = LatePayment.GetVouchers(campus, certificate, stage, grade, xclass, student, expenses);
                bool showIncompeteVoucher = incompeteVoucher.Count() > 0 ? true : false;
                ReportDataSource rdsIncompeteVoucher = new ReportDataSource("IncompeteVoucher", incompeteVoucher);

                var payments = LatePayment.GetPayments(campus, certificate, stage, grade, xclass, student, expenses);
                bool showPayments = payments.Count() > 0 ? true : false;
                ReportDataSource rdsPayments = new ReportDataSource("Payments", payments);

                var vouchers = Voucher.GetVouchersByStudent(student)
                    .Select(x => new { ExpenseName = x.ACC_Expense.Name, x.Date, x.Number, x.Value });
                bool showVouchers = vouchers.Count() > 0 ? true : false;
                ReportDataSource rdsVouchers = new ReportDataSource("Vouchers", vouchers);


                var returned = Voucher.GetReturnsByStudent(student)
                    .Select(x => new { x.Date, x.Id, x.Value, ExpenseName = x.ACC_Voucher.ACC_Expense.Name, VoucherDate = x.ACC_Voucher.Date, VoucherNumber = x.ACC_Voucher.Number, VoucherValue = x.ACC_Voucher.Value });
                bool showReturned = returned.Count() > 0 ? true : false;
                ReportDataSource rdsReturned = new ReportDataSource("Returned", returned);

                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(rdsBalance);
                ReportViewer1.LocalReport.DataSources.Add(rdsIncompeteVoucher);
                ReportViewer1.LocalReport.DataSources.Add(rdsPayments);
                ReportViewer1.LocalReport.DataSources.Add(rdsVouchers);
                ReportViewer1.LocalReport.DataSources.Add(rdsReturned);

                ReportParameter[] param = new ReportParameter[5];
                param[0] = new ReportParameter("BalanceParameter", showBalance.ToString(), true);
                param[1] = new ReportParameter("IncompeteVoucherParameter", showIncompeteVoucher.ToString(), true);
                param[2] = new ReportParameter("PaymentsParameter", showPayments.ToString(), true);
                param[3] = new ReportParameter("ReturnedParameter", showReturned.ToString(), true);
                param[4] = new ReportParameter("VouchersParameter", showVouchers.ToString(), true);
                this.ReportViewer1.LocalReport.SetParameters(param);
                ReportViewer1.LocalReport.Refresh();
            }
        }
    }
}